#!/bin/bash

# $Id$

usage="\n
Description:
	Shell script to create package and pull all the files for project submission\n
Usage:\n
$0\n
	-d : destination directory\n
	-r : src/ directory revision number (default is HEAD)\n
	-h : help (display this message)\n

Example:\n
	$0 -d X46010-TeamC-FinalProject -r 90\n
"

while getopts ":hd:r:" opt; do
  case $opt in
	d  ) export DEST_DIR=$OPTARG ;;
	r  ) export REV=$OPTARG ;;
	h  ) echo $usage
		 exit 0 ;;
	\? ) echo $usage
		 exit 0 ;;
  esac
done

shift $(($OPTIND - 1))

os=`uname`
SVN_URL=https://team-c-student-registration.googlecode.com/svn/trunk
SRC_URL=https://team-c-student-registration.googlecode.com/svn/trunk/src

echo ""
if [[ $REV == "" || $REV == "HEAD" ]]; then
	# find HEAD revision #
	export REV=`svn info $SVN_URL | grep ^Revision | awk '{print $2}'`
else
	export REV=${REV}
fi
dest_dir_suffix="-rev${REV}"

if [[ ${DEST_DIR} == "" ]]; then
	echo "Error: Destination directory not found."
	echo $usage
	exit 1
else
	if [[ ! -d ${DEST_DIR}${dest_dir_suffix} ]]; then
		echo "Destination directory ${DEST_DIR}${dest_dir_suffix} does not exist. Creating ..."
		/bin/mkdir ${DEST_DIR}${dest_dir_suffix}
	fi
fi

echo "Source code revision to export from repository is $REV."
echo "Exporting $SRC_URL rev#${REV} to ${DEST_DIR}${dest_dir_suffix} ..."
svn export -r $REV $SRC_URL ${DEST_DIR}${dest_dir_suffix}/src

echo ""
echo "Exporting README.TeamCFinalProject from $SVN_URL/docs ..."
svn export -r $REV $SVN_URL/docs/README.TeamCFinalProject ${DEST_DIR}${dest_dir_suffix}

# Create reports directory
if [[ ! -d ${DEST_DIR}${dest_dir_suffix}/reports ]]; then
	echo ""
	echo "${DEST_DIR}${dest_dir_suffix}/reports does not exist. Creating ..."
	/bin/mkdir ${DEST_DIR}${dest_dir_suffix}/reports
fi

echo ""
echo "Exporting TeamC Test Report.pdf from $SVN_URL/docs ..."
svn export -r $REV $SVN_URL/docs/TeamC%20Test%20Report.pdf ${DEST_DIR}${dest_dir_suffix}/reports

# Generate file history
ver_sum_rpt="${DEST_DIR}${dest_dir_suffix}/reports/VersionControlSummary.txt"
echo ""
echo "# Team C: Source Code Version Control Summary" > $ver_sum_rpt
src_files=`svn ls $SRC_URL`
for f in $src_files
do
	echo "Generating ${SRC_URL}/${f} file history ..."
	svn log ${SRC_URL}/${f} > ${DEST_DIR}${dest_dir_suffix}/reports/${f}.history
	echo "Mapping email to name in ${DEST_DIR}${dest_dir_suffix}/reports/${f}.history ..."
	if [[ $os == "Darwin" ]]; then
		sed -f email2name.sed  -i '' ${DEST_DIR}${dest_dir_suffix}/reports/${f}.history
	else
		sed -f email2name.sed  -i ${DEST_DIR}${dest_dir_suffix}/reports/${f}.history

	fi
	echo "Generating version control summary for ${SRC_URL}/${f} ..."
	echo "${f} contributors: " >> $ver_sum_rpt
	grep "^r[1-9]" ${DEST_DIR}${dest_dir_suffix}/reports/${f}.history | awk '{print $3, $4}' | sort -u >> $ver_sum_rpt
	echo "" >> $ver_sum_rpt
done

# 
echo "Concatenating ${SVN_URL}/docs/TeamC%20Test%20Report.pdf.contributors to $ver_sum_rpt ..."
svn cat -r $REV ${SVN_URL}/docs/TeamC%20Test%20Report.pdf.contributors >> $ver_sum_rpt

echo ""
echo "Done!"
